<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/12/29
 * Time: 11:00
 */
namespace app\province\controller;
use think\Db;
class Order extends Base
{
    public function workOrder(){
        if(request()->isAjax ()){
            extract(input());
            $map['w_fac_id'] = session('f_uid');
            if(isset($type)&&$type != ""){
                $map['p_status'] = $type;
            }
            if(isset($key)&&$key!="")
            {
                $map['w_server_name|w_work_name|p_name|p_num'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('work_order w')
                ->join('process p','w.w_id = p.p_w_id','left')
                ->where($map)
                ->count();
            $lists = Db::name('work_order w')
                ->join('process p','w.w_id = p.p_w_id','left')
                ->where($map)
                ->field (['w_id','w_server_name','w_type','w_work_name','w_create_time','w_content','p_num','p_name','p_uid','p_finish_name','p_new','p_status'])
                ->page($Nowpage,$limits)
                ->order('w_create_time desc')
                ->select ();
            foreach($lists as $key=>$vo){
                $z = [];
                if($vo['w_content'] != ''){
                    $link = trim_explode (',',$vo['w_content']);
                    foreach($link as $k=>$v){
                        $z[] = http_type () .$v;
                    }
                    $lists[$key]['w_content'] = implode (',',$z);
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch ('order/work_order');
    }

    /*
     * 创建工单
     */
    public function  orderAdd(){
        if(request ()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'w_server_name'=>$server_name,
                'w_work_name'=>$work_name,
                'w_type'=>$status,
                'w_fac_id'=>session('f_uid'),
                'w_pro_id'=>session('f_area'),
                'w_create_time'=>$time,
                'w_update_time'=>$time
            ];
            switch ($status){
                case 1:
                    $param['w_content'] = $photo;
                    break;
                case 2:
                    $param['w_content'] = $excel;
                    break;
                case 3:
                    $param['w_content'] = $pdf;
                    break;
            }
            Db::startTrans ();
            try{
                $id = Db::name('work_order')->insertGetId ($param);
                $user = trim_explode (',',$user);
                foreach ($user as $key=>$vo){
                    $data = Db::name('factory_member')->where('m_id',$vo)->find();
                    Db::name('process')->insert (['p_uid'=>$vo,'p_num'=>$data['m_serial'],'p_name'=>$data['m_name'],'p_w_id'=>$id]);
                }
                Db::commit ();
                return json(['code'=>200,'msg'=>'创建成功']);
            }catch (\Exception $e){
                Db::rollback ();
                return json(['code'=>100,'msg'=>'创建失败']);
            }
        }
        $user = Db::name('factory_member')->where('m_factory_id',session('f_uid'))->select ();
        return $this->fetch('order/order_add',['user'=>$user]);
    }


    /*
     * 继续发送
     */
    public function orderEdit(){
        $uid = Db::name('process')
            ->where('p_w_id',input('id'))
            ->column ('p_uid');
        if(request()->isPost ()){
            extract (input());
            Db::startTrans ();
            try{
                $user = trim_explode (',',$user);
                $else = array_diff($user,$uid);
                if(!empty($else)){
                    foreach($else as $key=>$vo){
                        $data = Db::name('factory_member')->where('m_id',$vo)->find();
                        $param[] = ['p_uid'=>$vo,'p_num'=>$data['m_serial'],'p_name'=>$data['m_name'],'p_w_id'=>$id];
                    }
                    Db::name('process')->insertAll ($param);
                }
                Db::commit();
                return json(['code'=>200,'msg'=>'编辑成功']);
            }catch (\Exception $e){
                Db::rollback ();
                return json(['code'=>100,'msg'=>'编辑失败']);
            }

        }
        $data = Db::name('work_order')
            ->where('w_id',input('id'))
            ->field(['w_id','w_server_name','w_work_name','w_type','w_content'])
            ->find ();
        $user = Db::name('factory_member')
            ->where('m_factory_id',session('f_uid'))
            ->select ();
        return $this->fetch ('order/order_edit',['data'=>$data,'uid'=>$uid,'user'=>$user]);
    }

    /*
     * 删除工单
     */
    public function orderDel(){
        Db::startTrans ();
        try{
            Db::name('process')->where('p_w_id',input('id'))->delete();
            Db::name('work_order')->delete(input('id'));
            Db::commit ();
            return json(['code'=>200,'msg'=>'删除成功']);
        }catch (\Exception $e){
            Db::rollback ();
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}